package com.lancoo.znbkxx.teacher.viewmodel

import android.app.Application
import androidx.lifecycle.MutableLiveData
import com.lancoo.cpbase.authentication.base.HandleCallback
import com.lancoo.znbkxx.teacher.Ext.Companion.ANALYSIS_URL
import com.lancoo.znbkxx.teacher.Ext.Companion.BASE_ADDRESS
import com.lancoo.znbkxx.teacher.Ext.Companion.BASE_URL
import com.lancoo.znbkxx.teacher.Ext.Companion.BASE_URL_COMMENT
import com.lancoo.znbkxx.teacher.Ext.Companion.CITY_ID
import com.lancoo.znbkxx.teacher.Ext.Companion.CNTOBSINFO
import com.lancoo.znbkxx.teacher.Ext.Companion.COMMENT_URL
import com.lancoo.znbkxx.teacher.Ext.Companion.COURSE_CLASSID
import com.lancoo.znbkxx.teacher.Ext.Companion.COURSE_CLASSNAME
import com.lancoo.znbkxx.teacher.Ext.Companion.CoursewareOBSInfo
import com.lancoo.znbkxx.teacher.Ext.Companion.GLOBAL_GRADE
import com.lancoo.znbkxx.teacher.Ext.Companion.INFOLIST
import com.lancoo.znbkxx.teacher.Ext.Companion.KNOWLEDGEPOINT_URL
import com.lancoo.znbkxx.teacher.Ext.Companion.OBSINFO
import com.lancoo.znbkxx.teacher.Ext.Companion.ONEONONE_URL
import com.lancoo.znbkxx.teacher.Ext.Companion.PLATFORM_URL
import com.lancoo.znbkxx.teacher.Ext.Companion.PROVINCE_ID
import com.lancoo.znbkxx.teacher.Ext.Companion.RESOURCE_URL
import com.lancoo.znbkxx.teacher.Ext.Companion.RZZD_URL
import com.lancoo.znbkxx.teacher.Ext.Companion.SCHOOLPAPER_URL
import com.lancoo.znbkxx.teacher.Ext.Companion.STAGE_NO
import com.lancoo.znbkxx.teacher.Ext.Companion.WORDS_URL
import com.lancoo.znbkxx.teacher.Ext.Companion.executeResponse
import com.lancoo.znbkxx.teacher.base.BaseViewModel
import com.lancoo.znbkxx.teacher.model.data.BaseInfo
import com.lancoo.znbkxx.teacher.model.repository.SystemRepository
import com.lancoo.znbkxx.teacher.utils.EncryptUtil
import com.lancoo.znbkxx.teacher.utils.InitDataListener
import com.lancoo.znbkxx.teacher.utils.MkUtils
import com.tencent.mmkv.MMKV
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import org.ksoap2.SoapEnvelope
import org.ksoap2.serialization.SoapObject
import org.ksoap2.serialization.SoapSerializationEnvelope
import org.ksoap2.transport.HttpTransportSE

/**
 * @author : Admin
 * @date   : 2019/7/22 9:54
 * @desc   :
 */
class UserInfoViewModel(application: Application) : BaseViewModel(application) {
    val mErrorMsg = MutableLiveData<String>()
    val UserName = MutableLiveData<String>()
    val PhotoPath = MutableLiveData<String>()


    private val repository by lazy { SystemRepository() }
    val baseInfo = MutableLiveData<MutableList<BaseInfo>>()

    companion object {
        const val NAMESPACE = "http://LGCPWS_Basic.org/"
        const val METHOD_NAME = "WS_G_GetSubSystemServerInfo"
    }


    fun getSystemConfigInfo(listener: HandleCallback?, initDataListener: InitDataListener?, token: String, subjectId: String, userID: String) {
        var mk = MMKV.defaultMMKV();
        launch {
            withContext(Dispatchers.IO) {
                val secCode = EncryptUtil.reverseMD5("P10")
                val soapAddress = getAddress(BASE_ADDRESS, secCode, "P10")
                val soapChildrenData = soapAddress!!.getProperty(0) as SoapObject
                val httpAdressDate = soapChildrenData.getProperty(3).toString()
                PLATFORM_URL = EncryptUtil.LgMgr_ParamDecrypt(httpAdressDate)
                mk?.encode("PLATFORM_URL", PLATFORM_URL)

                //                PLATFORM_URL ="http://172.16.41.231:15061/";//测试写死
                BASE_URL = PLATFORM_URL + "api/"
                mk?.encode("BASE_URL", BASE_URL)


                val secCode2 = EncryptUtil.reverseMD5("640")
                val soapAddress2 = getAddress(BASE_ADDRESS, secCode2, "640")
                val soapChildrenData2 = soapAddress2!!.getProperty(0) as SoapObject
                val httpAdressDate2 = soapChildrenData2.getProperty(3).toString()
                COMMENT_URL = EncryptUtil.LgMgr_ParamDecrypt(httpAdressDate2)
                mk?.encode("COMMENT_URL", COMMENT_URL)


                BASE_URL_COMMENT = COMMENT_URL + "api/ZNBK/"
                mk?.encode("BASE_URL_COMMENT", BASE_URL_COMMENT)


                val secCode3 = EncryptUtil.reverseMD5("p11")
                val soapAddress3 = getAddress(BASE_ADDRESS, secCode3, "p11")
                val soapChildrenData3 = soapAddress3!!.getProperty(0) as SoapObject
                val httpAdressDate3 = soapChildrenData3.getProperty(3).toString()
                ANALYSIS_URL = EncryptUtil.LgMgr_ParamDecrypt(httpAdressDate3)
                mk?.encode("ANALYSIS_URL", ANALYSIS_URL)


                val secCode5 = EncryptUtil.reverseMD5("p12")
                val soapAddress5 = getAddress(BASE_ADDRESS, secCode5, "p12")
                if (soapAddress5 != null) {
                    val soapChildrenData5 = soapAddress5.getProperty(0) as SoapObject
                    val httpAdressDate5 = soapChildrenData5.getProperty(3).toString()
                    ONEONONE_URL = EncryptUtil.LgMgr_ParamDecrypt(httpAdressDate5) + "api/"
                    mk?.encode("ONEONONE_URL", ONEONONE_URL)
                }

                val secCode6 = EncryptUtil.reverseMD5("p13")
                val soapAddress6 = getAddress(BASE_ADDRESS, secCode6, "p13")
                val soapChildrenData6 = soapAddress6!!.getProperty(0) as SoapObject
                val httpAdressDate6 = soapChildrenData6.getProperty(3).toString()
                WORDS_URL = EncryptUtil.LgMgr_ParamDecrypt(httpAdressDate6) + "api/"
                mk?.encode("WORDS_URL", WORDS_URL)


                val secCode7 = EncryptUtil.reverseMD5("831")
                val soapAddress7 = getAddress(BASE_ADDRESS, secCode7, "831")
                val soapChildrenData7 = soapAddress7!!.getProperty(0) as SoapObject
                val httpAdressDate7 = soapChildrenData7.getProperty(3).toString()
                RZZD_URL = EncryptUtil.LgMgr_ParamDecrypt(httpAdressDate7)
                mk?.encode("RZZD_URL", RZZD_URL)


            }
            val result = repository.getSystemConfigInfo(1, subjectId, token, userID)
            executeResponse(result, {
                KNOWLEDGEPOINT_URL = result.Data.KnowledgePointHttpAddr
                mk?.encode("KNOWLEDGEPOINT_URL", KNOWLEDGEPOINT_URL)

                RESOURCE_URL = result.Data.ResourceHttpAddr
                mk?.encode("RESOURCE_URL", RESOURCE_URL)

                OBSINFO = result.Data.OBSInfo
                CoursewareOBSInfo = result.Data.CoursewareOBS

                BASE_URL = result.Data.ApiUrl + "api/"
                mk?.encode("BASE_URL", BASE_URL)

                SCHOOLPAPER_URL = result.Data.PaperLibraryApiUrl + "api/"
                mk?.encode("SCHOOLPAPER_URL", SCHOOLPAPER_URL)

                PROVINCE_ID = result.Data.ProvinceID

                CITY_ID = result.Data.CityID

            }, {
                mErrorMsg.value = ""
                listener?.handleFail()
                initDataListener?.fail()
            })

            val cntObsInfo = repository.getObsInfo()
            executeResponse(cntObsInfo, { CNTOBSINFO = cntObsInfo },
                    {
                        mErrorMsg.value = ""
                        listener?.handleFail()
                        initDataListener?.fail()
                    })

            val classResult = repository.getBaseInfo("", token, userID, subjectId)
            executeResponse(classResult, {
                baseInfo.value = classResult.Data
                INFOLIST = classResult.Data
                INFOLIST?.let {
                    if (it.isNotEmpty()) {
                        GLOBAL_GRADE = it[0].GlobalGrade
                        MkUtils.encode("GLOBAL_GRADE", GLOBAL_GRADE)
                        STAGE_NO = when (GLOBAL_GRADE) {
                            "K6", "K7", "K8", "K9" -> "B"
                            else -> "C"
                        }
                        MkUtils.encode("STAGE_NO", STAGE_NO)
                        COURSE_CLASSID = it[0].CourseClassID
                        MkUtils.encode("COURSE_CLASSID", COURSE_CLASSID)
                        COURSE_CLASSNAME = it[0].CourseClassName
                        MkUtils.encode("COURSE_CLASSNAME", COURSE_CLASSNAME)
                    }
                }
                listener?.handleSuccess()
            }, {
                mErrorMsg.value = classResult.Message
                listener?.handleFail()
                initDataListener?.fail()
            })
        }
    }

    private fun getAddress(address: String, secCode: String, sysID: String): SoapObject? {
        try {
            val rpc = SoapObject(NAMESPACE, METHOD_NAME)
            //设置需要返回请求对象的参数
            rpc.addProperty("sysID", sysID)
            rpc.addProperty("subjectID", "")
            rpc.addProperty("secCode", secCode)

            val envelope = SoapSerializationEnvelope(SoapEnvelope.VER11)
            envelope.bodyOut = rpc
            envelope.dotNet = true
            envelope.setOutputSoapObject(rpc)

            val ht = HttpTransportSE(address + "Base/WS/Service_Basic.asmx")
            ht.debug = true
            val SOAP_ACTION = NAMESPACE + METHOD_NAME
            ht.call(SOAP_ACTION, envelope)
            if (envelope.response != null) {
                // 解析服务器响应的SOAP消息
                return envelope.bodyIn as SoapObject
            }

        } catch (e: Exception) {
            e.printStackTrace()
        }
        return null
    }
}